const Koa = require('koa');
const mysqls=require('./query/index.js')
var bodyParser = require('koa-bodyparser');
var Router = require('koa-router');
// response
const app = new Koa();


var router = new Router();
app.use(bodyParser());
app
  .use(router.routes())
  .use(router.allowedMethods());
const getList = async ctx=>{
    const $sql = `select * from model`
    const result = await mysqls.query($sql)
    ctx.body={
        code:1,
        result
    }
}
const insert = async ctx=>{
    const $sql = `INSERT INTO model username,tel,city,xcity VALUES (?,?,?,?)`
    const {username,tel,city,xcity} = ctx.request.body
    console.log(username,tel,city,xcity)
    const $params=[username,tel,city,xcity]
    const result = await mysqls.query($sql,$params)
    ctx.body={
        code:1,
        msg:"插入成功"
    }
}
const del = async ctx=>{
    const {id} = ctx.request.query
    const $sql = `DELETE FROM model WHERE id=?`
    const $params=[id]
    const result = await mysqls.query($sql,$params)
    ctx.body={
        code:1,
       msg:"删除成功"
    }
}
const updata = async ctx=>{
    // const $sql = `UPDATE model SET username=?,tel=?,city=?,xcity=? WHERE id=?`
    const $sql = 'update model set username=?,tel=?,city=?,xcity=? where id=?';
    const {username,tel,city,xcity,id} = ctx.request.body
    console.log(username,tel,city,xcity)
    const $params=[username,tel,city,xcity,id]
    const result = await mysqls.query($sql,$params)
    ctx.body={
        code:1,
        msg:"更新成功"
    }
}
router.get('/get',getList);
router.post('/insert',insert);
router.get('/del',del);
router.post('/updata',updata);


app.listen(3000);